/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.aop.handler.cglib;

import java.lang.reflect.Method;
import java.util.logging.Logger;

import com.hyts.aop.bean.ProcessArg;
import com.hyts.aop.handler.base.AfterMethod;
import com.hyts.aop.handler.base.BeforeMethod;
import com.hyts.aop.handler.base.RoundMethod;
import com.hyts.aop.handler.base.ValidateMethod;
import com.hyts.aop.handler.reflection.*;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/** 
 * @author LiBo/Alex
 * @see net.sf.cglib.proxy.MethodInterceptor
 * @since  V1.0
 * @version V1.0 
 */
public class MethodInterceptorHandler implements MethodInterceptor  {
    
    private BeforeMethod beforeMethodHandler;

    private AfterMethod afterMethodHandler;

    private RoundMethod roundMethodHandler;

    private ValidateMethod pointcutMethodHandler;

    private Object targetObject;

    /**
     * @fieldName logger
     * @fieldType Logger
     */
    Logger logger = Logger.getLogger(BeforeMethodHandler.class.getName());

    /**
     * @constructor：BeforeAopHandler
     * @param proxyRealObject
     */
    public MethodInterceptorHandler(Object proxyRealObject) {
        ReflectionFacadeHandler.setBeforeMethodHandler(new BeforeMethodHandler(proxyRealObject));
        ReflectionFacadeHandler.setAfterMethodHandler(new AfterMethodHandler(proxyRealObject));
        ReflectionFacadeHandler.setRoundMethodHandler(new RoundMethodHandler(proxyRealObject));
        ReflectionFacadeHandler.setPointcutMethodHandler(new ValidateMethodHandler(proxyRealObject));
    }
    
    /**
     * @constructor：BeforeAopHandler
     * @param proxyRealObject
     */
    public MethodInterceptorHandler(Object proxyRealObject,Object... aopWeaveObject) {
        targetObject = proxyRealObject;
        ReflectionFacadeHandler.setBeforeMethodHandler(new BeforeMethodHandler(aopWeaveObject));
        ReflectionFacadeHandler.setAfterMethodHandler(new AfterMethodHandler(aopWeaveObject));
        ReflectionFacadeHandler.setRoundMethodHandler(new RoundMethodHandler(aopWeaveObject));
        ReflectionFacadeHandler.setPointcutMethodHandler(new ValidateMethodHandler(proxyRealObject));
    }
    
    /* 
     * @param obj
     * @param method
     * @param args
     * @param proxy
     * @return
     * @throws Throwable
     * @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], net.sf.cglib.proxy.MethodProxy)  
     * @exception
     */ 
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        //校验是否被选中为忽略状态
        Object result = null;
        ProcessArg processArg = null;
        if(ReflectionFacadeHandler.getPointcutMethodHandler().validate(method)) {
            ReflectionFacadeHandler.getBeforeMethodHandler().before(args);
            processArg = ReflectionFacadeHandler.getRoundMethodHandler().round(method,targetObject,args);
        }
       /* if(processArg != null && !processArg.isExecuted()) {

        }*/
        result = proxy.invokeSuper(obj, args);
        ReflectionFacadeHandler.getRoundMethodHandler().round(method,targetObject,args);
        if(ReflectionFacadeHandler.getPointcutMethodHandler().validate(method)) {
            ReflectionFacadeHandler.getAfterMethodHandler().after(args);
        }
        return result;
    }

}
